package com.example.criminalintent.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.criminalintent.CrimeActivity;
import com.example.criminalintent.CrimePagerActivity;
import com.example.criminalintent.R;
import com.example.criminalintent.model.Crime;
import com.example.criminalintent.model.CrimeLab;

import java.text.SimpleDateFormat;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class CrimeListFragment extends Fragment {

    private RecyclerView mCrimeRecyclerView;
    private CrimeAdapter mAdapter;

    private RelativeLayout relativeLayout;
    private Button addButton;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_crime_list,container,false);
        mCrimeRecyclerView = view.findViewById(R.id.crime_recycler_view);
        mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        relativeLayout = view.findViewById(R.id.no_crimes_layout);
        addButton = view.findViewById(R.id.add_button);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addNewCrime();
            }
        });
        updateUI();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    private void updateUI() {

        int crimes = adapterIsNull();
        if(crimes>0) {
            mCrimeRecyclerView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.GONE);
            mAdapter.notifyDataSetChanged();
        }else {
            mCrimeRecyclerView.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.VISIBLE);
        }
    }

    private int adapterIsNull(){
        CrimeLab crimeLab = CrimeLab.getsCrimeLab(getActivity());
        List<Crime> crimes = crimeLab.getCrimes();
        if(mAdapter == null){
            mAdapter = new CrimeAdapter(crimes);
            mCrimeRecyclerView.setAdapter(mAdapter);
        }
        mAdapter.setCrimes(crimes);
        return crimes.size();
    }


    private class CrimeHolder extends RecyclerView.ViewHolder{

        private TextView mTitleTextView;
        private TextView mDateTextView;
        private View itemView;
        private ImageView mSolvedImageView;

        private Context context;
        public CrimeHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            mTitleTextView = itemView.findViewById(R.id.crime_title);
            mDateTextView = itemView.findViewById(R.id.crime_data);
            mSolvedImageView = itemView.findViewById(R.id.crime_solved);
            mTitleTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),mTitleTextView.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });


            
        }


    }

    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{

        private List<Crime> mCrimes;
        private View itemView;

        public void setCrimes(List<Crime> crimes){
            mCrimes = crimes;
        }

       public CrimeAdapter(List<Crime> crimes){
           mCrimes = crimes;
       }

        @NonNull
        @Override
        public CrimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
           LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
           View view = layoutInflater.inflate(R.layout.list_item_crime,parent,false);
           return new CrimeHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull CrimeHolder holder, final int position) {
                final Crime crime = mCrimes.get(position);
            SimpleDateFormat f = new SimpleDateFormat("yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒");
                holder.mTitleTextView.setText(crime.getmTitle());
                holder.mDateTextView.setText(f.format(crime.getmDate()));
                holder.mSolvedImageView.setVisibility(crime.ismSolved()
                ?View.VISIBLE: View.GONE);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = CrimePagerActivity.newIntent(getActivity(),crime.getmId());
                        intent.putExtra("position",position);
                        startActivity(intent);
                    }
                });
       }

        @Override
        public int getItemCount() {
            return mCrimes.size();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI();
        updateSubtitle();
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime_list,menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
       switch (item.getItemId()){
           case R.id.new_crime:
               addNewCrime();
               return true;
           case R.id.show_subtitle:
               updateSubtitle();
               return true;

       }

        return super.onOptionsItemSelected(item);
    }

    private void addNewCrime() {
        Crime crime = new Crime();
        CrimeLab.getsCrimeLab(getActivity()).addCrime(crime);
        Intent intent  = CrimePagerActivity.newIntent(getActivity(),crime.getmId());
        startActivity(intent);
    }

    private void updateSubtitle(){
        CrimeLab crimeLab = CrimeLab.getsCrimeLab(getActivity());
        int crimeCount = crimeLab.getCrimes().size();
        String subTitle = getResources().getQuantityString(R.plurals.subtitle_plural,crimeCount,crimeCount);
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.getSupportActionBar().setSubtitle(subTitle);
    }


}
